package com.aishare.qicaitaoke.ui;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;

import com.aishare.qicaitaoke.R;
import com.aishare.qicaitaoke.base.BaseActivity;
import com.aishare.qicaitaoke.ui.fragment.BrandFragment;
import com.aishare.qicaitaoke.ui.fragment.CenterFragment;
import com.aishare.qicaitaoke.ui.fragment.IndexFragment;
import com.aishare.qicaitaoke.ui.fragment.MeFragment;
import com.aishare.qicaitaoke.ui.fragment.VideoFragment;
import com.aishare.qicaitaoke.ui.view.BottomBar;

import java.util.List;

public class MainActivity extends BaseActivity implements BottomBar.OnBottonbarClick{

    private IndexFragment indexFragment;
    private VideoFragment videoFragment;
    private CenterFragment centerFragment;
    private BrandFragment brandFragment;
    private MeFragment meFragment;
    private BottomBar bottomBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFragment(savedInstanceState);
        init();
    }

    private void init(){
        bottomBar = findViewById(R.id.tab_bottom);
        bottomBar.setOnBottombarOnclick(this);

    }

    private void initFragment(Bundle savedInstanceState){
        if(savedInstanceState!=null){
            //异常情况
         List<Fragment> mFragments = getSupportFragmentManager().getFragments();
         for (Fragment fragment:mFragments){
             if(fragment instanceof IndexFragment){
                 indexFragment = (IndexFragment) fragment;
             }
             if(fragment instanceof VideoFragment){
                 videoFragment = (VideoFragment) fragment;
             }
             if(fragment instanceof CenterFragment){
                 centerFragment = (CenterFragment) fragment;
             }
             if(fragment instanceof  BrandFragment){
                 brandFragment = (BrandFragment) fragment;
             }
             if(fragment instanceof MeFragment){
                 meFragment = (MeFragment) fragment;
             }
         }
        }else {
            indexFragment = new IndexFragment();
            videoFragment = new VideoFragment();
            brandFragment = new BrandFragment();
            centerFragment = new CenterFragment();
            meFragment = new MeFragment();
            FragmentTransaction fragmentTrans =  getSupportFragmentManager().beginTransaction();
            fragmentTrans.add(R.id.main_pager,indexFragment);
            fragmentTrans.add(R.id.main_pager,videoFragment);
            fragmentTrans.add(R.id.main_pager,centerFragment);
            fragmentTrans.add(R.id.main_pager,brandFragment);
            fragmentTrans.add(R.id.main_pager,meFragment);
            fragmentTrans.commit();
        }
        getSupportFragmentManager().beginTransaction().show(indexFragment)
                .hide(videoFragment)
                .hide(centerFragment)
                .hide(brandFragment)
                .hide(meFragment)
                .commit();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }


    @Override
    public void onIndexClick() {
        getSupportFragmentManager().beginTransaction().show(indexFragment)
                .hide(videoFragment)
                .hide(centerFragment)
                .hide(brandFragment)
                .hide(meFragment)
                .commit();
    }

    @Override
    public void onVideoClick() {
        getSupportFragmentManager().beginTransaction().show(videoFragment)
                .hide(indexFragment)
                .hide(centerFragment)
                .hide(brandFragment)
                .hide(meFragment)
                .commit();
    }

    @Override
    public void onBrandClick() {
        getSupportFragmentManager().beginTransaction().show(brandFragment)
                .hide(indexFragment)
                .hide(videoFragment)
                .hide(centerFragment)
                .hide(meFragment)
                .commit();
    }

    @Override
    public void onMeClick() {
        getSupportFragmentManager().beginTransaction().show(meFragment)
                .hide(indexFragment)
                .hide(videoFragment)
                .hide(centerFragment)
                .hide(brandFragment)
                .commit();
    }

    @Override
    public void onCenterClick() {
        getSupportFragmentManager().beginTransaction().show(centerFragment)
                .hide(indexFragment)
                .hide(videoFragment)
                .hide(brandFragment)
                .hide(meFragment)
                .commit();
    }
}
